package com.windy.dailynews.presenter.impl;

import com.google.gson.Gson;
import com.windy.dailynews.model.NewsDetail;
import com.windy.dailynews.presenter.NewsDetailContract;
import com.windy.dailynews.request.DataRequest;

import org.json.JSONObject;

/**
 * Created by dingfeng on 2016/11/21.
 */
public class NewsDetailPresenter implements NewsDetailContract.Presenter {

    private final NewsDetailContract.View mView;

    public NewsDetailPresenter(NewsDetailContract.View view) {
        mView = view;
        mView.setPresenter(this);
    }

    @Override
    public void loadData(int id) {
        mView.setLoading();
        DataRequest.getInstance().getNewsContent(id, new DataRequest.Callback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject data) {
                if (data != null) {
                    Gson gson = new Gson();
                    NewsDetail detail = gson.fromJson(data.toString(), NewsDetail.class);
                    mView.showNewsContent(detail);
                } else {
                    mView.setError();
                }
            }

            @Override
            public void onError(String err) {
                mView.setError();
            }
        });
    }

    @Override
    public void start() {

    }

}
